package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func TestController(router *gin.Engine) {
	router.GET("/index", func(context *gin.Context) {
		context.String(http.StatusOK, "hello golang hello gin")
	})

	router.GET("/redirect", func(context *gin.Context) {
		context.Redirect(http.StatusMovedPermanently, "/index")
	})

	router.GET("/goindex", func(context *gin.Context) {
		context.Request.URL.Path = "/index"
		router.HandleContext(context)
	})

	router.GET("/api/gin", func(context *gin.Context) {
		result := map[string]string{"message": "ok"}

		param1 := context.Query("param1")
		if param1 != "" {
			result["param1"] = param1
		}
		param2 := context.Query("param2")
		if param2 != "" {
			result["param2"] = param2
		}

		context.JSON(http.StatusOK, result)
	})

	router.GET("/api/gin/:name", func(context *gin.Context) {
		name := context.Param("name")

		context.JSON(http.StatusOK, gin.H{
			"message": "ok",
			"data": name,
		})
	})

	router.POST("/api/gin", func(context *gin.Context) {
		username := context.PostForm("username")
		password := context.DefaultPostForm("password", "000000")

		if username == "" {
			panic("this is error")
		}

		context.JSON(http.StatusOK, gin.H{
			"message": "ok",
			"username": username,
			"password": password,
		})
	})

	router.POST("/api/mix/:name", func(context *gin.Context) {
		name := context.Param("name")
		summary := context.Query("summary")
		username := context.PostForm("username")

		context.JSON(http.StatusOK, gin.H{
			"message": "ok",
			"name": name,
			"summary": summary,
			"username": username,
		})
	})
}
